package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.approve.model.Apl_target;
import com.power.sifa.approve.model.pageInfo.Apl_targetPageInfo;
import com.power.sifa.approve.api.IApl_targetFeign;
import com.power.sifa.approve.service.Apl_targetService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   被申请人控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_targetController",tags={"被申请人"})
public class Apl_targetController implements IApl_targetFeign{

    @Resource
    private Apl_targetService apl_targetService;

    
    @Override
    @ApiOperation("查询被申请人分页列表")
    @SaCheckPermission("apl_target_list")
    public ResponseDTO list(Apl_targetPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_target> page = this.apl_targetService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_target apl_target : page) {
                apl_target.geneText();
                list.add(apl_target);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看被申请人")
    @SaCheckPermission("apl_target_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_target apl_target  = apl_targetService.getById(id);
        if (apl_target != null) {
            apl_target.geneText();
        }
        return ResponseDTO.succData(apl_target);
    }

    @Override
    @ApiOperation("删除被申请人")
    @SaCheckPermission("apl_target_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_targetService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改被申请人")
    @SaCheckPermission("apl_target_save")
    public ResponseDTO save(Apl_target apl_target) {
        Assert.notNull(apl_target, "apl_target不能为空");
        apl_targetService.saveOrUpdate(apl_target);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除被申请人")
    @SaCheckPermission("apl_target_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_targetService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_target_list")
    public void output(HttpServletResponse response, Apl_targetPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "被申请人";
        String[] heads = {"applyid+nanoid","申请id","名称","行政区划代码"};
        String[] fields = {"id","applyid","name","adid"};
        Page<Apl_target> page = this.apl_targetService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "被申请人";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid+nanoid","申请id","名称","行政区划代码"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyid","name","adid"};
        List<Apl_target> list = (List<Apl_target>)ExcelUtil.Import(fields, file, Apl_target.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_targetService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO getTargetListByApplyid(String applyid) {
        Assert.notNull(applyid, "applyid不能为空");
        return ResponseDTO.succData(this.apl_targetService.getTargetListByApplyid(applyid));
    }

}
